<?php

/*

    MMetaverse - Open Metaverse Access Library

    Copyright (c) 2009 Mary Jane Soft - Marlon J. Manrique

    http://www.maryjanesoft.com
    http://www.marlonj.com

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General
    Public License along with this library; if not, write to the
    Free Software Foundation, Inc., 59 Temple Place, Suite 330,
    Boston, MA  02111-1307  USA

    $Id$

*/

/**
 * @package mjs.mmetaverse.manager
 */

require_once dirname(__FILE__) . '/../inventory/MFolder.class.php';
require_once dirname(__FILE__) . '/../inventory/MInventory.class.php';

require_once 'MDataBase.class.php';

/**
 * Inventory Data Base
 */
class MInventoryDB extends MDataBase {
    
    /**
     * Create the database access for the OpenMetaverse object specified
     *
     * @param MMetaverse $metaverse current metaverse
     */
    public function MInventoryDB(MMetaverse $metaverse) {
        $this->MDataBase($metaverse);
        $this->db = $this->getDatabase('inventory');
    }

    /**
     * Returns the Inventory Structure for the user specified
     *
     * @param MUUID $userUUID The user uuid
     * @return MInvetory The user inventory 
     */
    public function getInventory(MUUID $userUUID)
    {
        // Get the root folder 
        $rootFolder = $this->getRootFolder($userUUID);

        // Get Childs 
        $childs = $this->getChildFolders($rootFolder->getId());

        // Add Childs to the root folder 
        foreach ($childs as $folder) {
            $rootFolder->addFolder($folder);
        }

        // Create inventory associated with the inventory manager 
        $inventory = new MInventory(null,
            $this->metaverse->getInventoryManager());

        // Set root folder 
        $inventory->setRootFolder($rootFolder);

        // Return Inventory
        return $inventory;
    }

    /**
     * Returns the root folder for the user specified
     *
     * @param MUUID $userUUID The user uuid
     * @return MFolder The root folder for the user
     */
    private function getRootFolder(MUUID $userUUID) {
        // Select the folder with parent zero 
        $sql = 'select uuid, name, agentid, parentid, type, version ' .
            'from inventoryfolders where ' .
            'agentID=? and parentid=\'00000000000000000000000000000000\'';
        
        $stmt = $this->prepare($this->db, $sql);

        // Set parameters
        $stmt->bindValue(1,$userUUID->getUnHyphenated());
        $this->execute($stmt);

        // Get Result
        $result = $this->fetch($stmt);

        // Get the result info on a folder object 
        return $this->fecthFolderInfo($result);
    }

    /**
     * Returns the childs folder of the folder specified 
     *
     * @param MUUID $parentUUID Parent folder
     * @return MFolder[] The list with the childs folders 
     */
    private function getChildFolders(MUUID $parentUUID) {
        // Select the folder with parent zero
        $sql = 'select uuid, name, agentid, parentid, type, version ' .
            'from inventoryfolders where parentID=?';
        $stmt = $this->prepare($this->db, $sql);

        // Set parameters
        $stmt->bindValue(1,$parentUUID->getUnHyphenated());
        $this->execute($stmt);

        // Load until exception is throw
        while(true)
        {
            try {
                // Load folders and add to the array
                $result = $this->fetch($stmt);
                $folders[] = $this->fecthFolderInfo($result);
            } catch (Exception $e) {
                // No more users
                break;
            }
        }

        // Get list of folders
        return $folders;
    }

    /**
     * Load the data from a folder query info a folder object
     *
     * @param array $result Query result
     * @return MFolder The folder object 
     */
    private function fecthFolderInfo(array $result)
    {
        // Get attributes 
        $uuid = new MUUID($result[0]);
        $name = $result[1];
        $agentID = new MUUID($result[2]);
        $parentID = new MUUID($result[3]);
        $type = (integer) $result[4];
        $version = (integer) $result[5];

        // Create a new folder with the info
        $folder = new MFolder($uuid, $name,
            $parentID, $type, $version, $agentID);

        // Return the folder
        return $folder;
    }
}

?>